package day4;
//克隆对象
//1.实现Cloneable接口
public
class TestClone  {
    public static
    void main(String[] args) throws CloneNotSupportedException {
        Person p=new Person("罗浩",22,"男",100000);
        Student s=new Student("彭正",22);
        Student s1=s.clone();
        p.student=s1;
        Person p1=p.clone();
        p1.name="周正";
        p1.student.name="周正";
        System.out.println(s);
        System.out.println(s1);
        System.out.println(p+" "+p.student);
        System.out.println(p1+" "+p1.student);
    }
}
class Student implements Cloneable {
    String name;
    int age;
    protected Student clone() throws CloneNotSupportedException {
        return (Student) super.clone();
    }

    public
    Student(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("构造方法");
    }

    public
    Student() {
    }


}
class Person implements Cloneable {
    String name;
    int age;
    String sex;
    int id;
    Student student;

    public
    Person(String name, int age, String sex, int id) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.id = id;
    }

    protected Person clone() throws CloneNotSupportedException {
       Person p=(Person) super.clone();
       p.student=student.clone();
      return p;
   }

    public
    Person() {
    }

    public String toString() {
        return "name:" + name + " age:" + age + " sex:" + sex + " id:" + id ;
    }
}